﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using webtracuudiem.Truyxuatdulieu;
using System.Web.Security;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

namespace webtracuudiem
{
    public partial class quanlydiem : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButThoat_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("/Default.aspx");
        }

        
        protected void ButCapnhat_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                if (row.RowType != DataControlRowType.DataRow)
                    continue;
                HiddenField hidenMaSV = (HiddenField)row.FindControl("hidenMaSV");
                string masv = hidenMaSV.Value;
                TextBox txtLan1 = (TextBox)row.FindControl("txtDiemL1");
                object diemLan1;
                if (string.IsNullOrEmpty(txtLan1.Text))
                    diemLan1 = null;
                else
                    diemLan1 = float.Parse(txtLan1.Text);
                TextBox txtLan2 = (TextBox)row.FindControl("txtDiemL2");
                object diemLan2;
                if (string.IsNullOrEmpty(txtLan2.Text))
                    diemLan2 = null;
                else
                    diemLan2 = float.Parse(txtLan2.Text);
                string maMH = DropMon.SelectedValue;
                string sqlSelect = "SELECT * FROM Diem WHERE MaSV=@MaSV AND MaMH=@MaMH";
                DataTable tbl = TienIchSql.ChayLenhSelectDuLieu(sqlSelect, "@MaMH", maMH, "@MaSV", masv);
                string sql;
                if (tbl.Rows.Count == 0)
                    sql = "INSERT INTO Diem(MaSV,MaMH,DiemL1,DiemL2) VALUES(@MaSV,@MaMH,@DiemL1,@DiemL2)";
                else
                    sql = "UPDATE Diem SET DiemL1=@DiemL1,DiemL2=@DiemL2 WHERE MaSV=@MaSV AND MaMH=@MaMH";
                string[] mangTenThamSo = new string[] { "@MaSV", "@MaMH", "@DiemL1", "@DiemL2" };
                object[] mangGiaTriThamSo = new object[] { masv, maMH, diemLan1, diemLan2 };
                SqlDbType[] mangKieuThamSo = new SqlDbType[] { SqlDbType.Char, SqlDbType.Char, SqlDbType.Float, SqlDbType.Float };
                TienIchSql.ChayLenhCapNhatDuLieu(sql, mangTenThamSo, mangGiaTriThamSo, mangKieuThamSo);
            }
            //Response.Redirect(Request.RawUrl);
            GridView1.DataBind();
        }

        protected void ButHienthi_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
            //Response.Redirect(Request.RawUrl);
            
        }


    }
}